//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

#ifndef __VOTERLIB_H__
#define __VOTERLIB_H__

#include <time.h>
#include "math_utils.h"
#include "voter_server.h"
#include "voter_message.h"
#include "rudp.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define NANOSECONDS_FOR_SCHEDULE    20

int checkArguments(int argc, char **argv);
int CreateTCPServer(int port, const char *listenNs);
int createTCPConnection(const char *host, int port);
int CreateUDPServer(int port, const char *listenNs);
int CreateUDPClient(int port);
void udp_send(RudpPackage *p, int sock_fd, struct sockaddr_in serv_addr);
void dump_recv(Rudp *U);

int SocketSetNonBlockNoDelay(int fd);
int AcceptClient(int server_socket, struct sockaddr_in *sa);
int TCPConnect(const char *addr, int port, int nonblock);
int ReadNBytes(int socket, char* buffer, int bufferSize);

void printVoterMessage(VoterMessage *msg);
void printVoterMsg(const char *prompt, VoterMessage *msg);
VoterMessage *sendVoterMessage(int s, char type, uint64_t id_trans, 
                              uint64_t lam_clock, int64_t data, char *buf,
                              int32_t rawDataSize, bool isTCP);
void print_hex(char *readbuf, int nread);

int WelcomeToServer(VoterClient *c);
int ResponseZMQ(int fd, bool isZMQ);

int TimespecWaitNanoseconds(struct timespec *ts, int64_t *delayTime,
                                                              long nanoSeconds);

#ifdef __cplusplus
}
#endif

#endif // __VOTERLIB_H__
